package com.jhc.drone.communication.processor;


import com.jhc.drone.communication.datagram.receive.Receiver;
import com.jhc.drone.communication.enums.ProtocolEnum;
import com.jhc.drone.communication.mavlink.MAVLinkPacket;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author lcp
 * @date 2025年01月2025/1/7 10:06
 * @describe
 */
@Slf4j
public class UnpackMessageProcessor implements MessageProcessor {

    private final ProtocolEnum protocol;


    public UnpackMessageProcessor(ProtocolEnum protocolEnum) {
        this.protocol = protocolEnum;
    }

    @Override
    public void unpack(ByteBuf byteBuf, Channel channel) {
        byte[] data = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(data);
        StringBuilder hexString = new StringBuilder();
        // 将字节数据转换为十六进制格式
        for (byte b : data) {
            hexString.append(String.format("%02X ", b & 0xFF));
        }
//        log.info("receive data-----------{}", hexString);
        String[] message = hexString.toString().split(" ");
        for (String str : message) {
            MAVLinkPacket mavLinkPacket = parser.mavlink_parse_char(Integer.decode("0x" + str));
            if (Objects.nonNull(mavLinkPacket)) {
                try {
                    Receiver.parse(channel, mavLinkPacket);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        }

    }

    @Override
    public byte[] pack(Object data) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void send(byte[] data, String sn) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void sendBySysId(byte[] data, Integer sysId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
